/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function() {
    cate.init();
});

var cate = (function() {

    function addNewCate() {
        var cateName = $('#cateName').val();

        $('#cateInput .form-group').removeClass('has-error');
        $('#cateAlert').hide().html('');
        if (!cateName) {
            $('#cateName').parent('.form-group').addClass('has-error');
            return false;
        }

        $.post('/newscate/ajax-add-new-newscate', {
            name: cateName
        }, function(resp) {
            if (resp.err == 0) {
                var data = resp.data,
                    html = '<tr>\
                                <td class="name">\
                                    <a data-title="Tên danh mục" data-pk="' + data.id + '" data-type="text" href="#" class="editable editable-click">\
                                    ' + cateName + '\
                                    </a>\
                                </td>\
                                <td class="text-center">\
                                    <a href="#" class="text-danger" onclick="return cate.deleteCate(this, \'' + data.id + '\')"><i class="fa fa-trash-o"></i></a>\
                                </td>\
                            </tr>';
                $('#listCate').find('tbody').prepend(html);
                $('#cateName').val('');
            } else {
                $('#cateAlert').show().html(resp.msg);
            }
            $('#addCate').find('i.fa').attr('class', 'fa fa-plus-circle');
        }, 'json');
    }

    function initHandler() {
        $('#addCate').click(function() {
            $(this).find('i.fa').attr('class', 'fa fa-spinner fa-spin');
            addNewCate();
            return false;
        });

        $('#listCate').editable({
            selector: 'a.cate_name',
            mode: 'inline',
            url: '/newscate/ajax-edit-newscate',
            pk: 1,
                ajaxOptions: {
                type: 'post',
                dataType: 'json'
            }
        });
    }

    return {
        init: function() {
            initHandler();
        },
        
        deleteCate: function(obj, id) {
            var tr = $(obj).parents('tr'),
                name = tr.find('td.name').find('a').html();
            showConfirm({
                msg: 'Bạn có chắc chắn muốn xoá danh mục <span class="text-danger">"' + name + '"</span> không?',
                title: 'Xoá danh mục',
                noBt: 'Đóng',
                yesBt: 'Đồng ý xoá',
                onYes: function(iid) { 
                    $.post('/newscate/ajax-delete-newscate', {id: iid}, function(resp) {
                        if(resp.err == 0) {
                            tr.remove();
                            hideConfirm();
                        }
                    }, 'json');
                },
                param: id
            });
            return false;
        }
    };
})();